package com.hellojava.util.web;

import com.google.common.collect.Maps;
import org.springframework.web.method.HandlerMethod;

import java.util.Map;
import java.util.Set;

public class HttpMethodInfo {
    private static final Map<String, HttpMethodInfo> methodInfoMap = Maps.newHashMap();

    private boolean isAjax = false;

    private String ajaxRedirectReturnUri;

    private boolean needLogin = false;

    private Set<String> allowPermissions;

    private String method;

    public static HttpMethodInfo parse(HandlerMethod method) {
        HttpMethodInfo result = methodInfoMap.get(getMethodMapKey(method));
        if (result != null) {
            return result;
        }

        result = new HttpMethodInfo();
        AjaxMapping am = method.getMethodAnnotation(AjaxMapping.class);
        if (am != null) {
            result.isAjax = true;
            result.ajaxRedirectReturnUri = am.currentPageUri();
        }


        methodInfoMap.put(getMethodMapKey(method), result);
        return result;
    }

    private static String getMethodMapKey(HandlerMethod method) {
        return method.getBeanType().getName() + ":" + method.getMethod().getName();
    }

    public boolean isAjax() {
        return isAjax;
    }

    public String getAjaxRedirectReturnUri() {
        return ajaxRedirectReturnUri;
    }

    public boolean isNeedLogin() {
        return needLogin;
    }

}
